package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.User;
import com.gxa.zhonglong.service.UserService;
import com.gxa.zhonglong.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author ws
 * @date 2021/10/21 10:38
 */
@Api(tags = "员工接口的api")
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService = null;

    @GetMapping("/selectByName")
    @ApiOperation("根据姓名/用户名模糊查询")
    public Map<String,Object> selectByName(String name) throws BusinessException {
        final List<Map<String, Object>> maps = userService.selectByName(name);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    @GetMapping("/selectByPage")
    @ApiOperation("分页查询查询")
    public Map<String,Object> selectByPage(Integer page,Integer size,String name) throws BusinessException {
        final List<Map<String, Object>> maps = userService.selectByPage(page, size,name);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setData(maps);
        jsonResult.put("count",userService.selectCount(name));
        return jsonResult.getDataMap();
    }

    @PostMapping("/addOrUpdate")
    @ApiOperation("添加/修改员工")
    public Map<String,Object> addOrUpdate(User user) throws BusinessException {
        System.out.println(user);
        JsonResult jsonResult=new JsonResult();
        if(!StringUtil.isEmpty(user.getPhone()) && !StringUtil.isPhoneNumber(user.getPhone())){
            throw new BusinessException(ExceptionCode.BAD_PHONE);
        }
        if(user.getId()==null){
            userService.add(user);
            jsonResult.setMsg("添加成功!");
        }else {
            userService.update(user);
            jsonResult.setMsg("修改成功!");
        }
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/del")
    @ApiOperation("删除员工")
    public Map<String,Object> del(@RequestBody Map<String,List<Long>> ids) throws BusinessException {
        userService.del(ids.get("ids"));

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功!");
        return jsonResult.getDataMap();
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "用户名",required = true, dataType = "String"),
            @ApiImplicitParam(name = "pwd",value = "用户密码",required = true, dataType = "String")

    })
    public Map<String,Object> login(String userName, String pwd,@ApiIgnore("session") HttpSession session) throws BusinessException {
        System.out.println(userName);
        System.out.println(pwd);
        Map<String, Object> login = userService.login(userName, pwd);

        // 利用UUID生成token
        String token = UUID.randomUUID().toString().replace("-","");
        session.setAttribute("token",token);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        login.put("token",token);
        jsonResult.setData(login);
        jsonResult.setMsg("登录成功");

        return jsonResult.getDataMap();
    }

    @PostMapping("/logout")
    @ApiOperation("用户退出")
    public Map<String,Object> logout(@ApiIgnore("session") HttpSession session){

        // 清除session对象中的token
        session.removeAttribute("token");

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/test")
    @ApiOperation("判断登录否")
    public String test(){

        return "ok";
    }

}
